﻿package com.controller
{
    import flash.events.*;

    /**
     * 游戏自定义事件主类
     * @author xiezhicheng
     */
    public class GameDispatcher extends Object
    {
        private var eventDispatcher:IEventDispatcher;
        private static var instance:GameDispatcher;

		
        /**
         * 
         * @param target
         * @throws Error
         */
        public function GameDispatcher(target:IEventDispatcher = null)
        {
            if (instance != null)
            {
                throw new Error("this is a single instance class");
            }
            instance = this;
            this.eventDispatcher = new EventDispatcher(target);
        }
		
        /**
         * 建立侦听
         * @param type	事件类型
         * @param listener	回调函数
         * @param useCapture
         * @param priority
         * @param useWeakReference
         */
        public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false) : void
        {
            this.eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }

		
        /**
         * 移除侦听
         * @param type
         * @param listener
         * @param useCapture
         */
        public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false) : void
        {
            this.eventDispatcher.removeEventListener(type, listener, useCapture);
        }

		
        /**
         * 派发事件
         * @param event
         * @return 
         */
        public function dispatchEvent(event:Event) : Boolean
        {
            return this.eventDispatcher.dispatchEvent(event);
        }

		
        /**
         * 检测是否为type事件注册了侦听器
         * @param type	事件类型
         * @return 
         */
        public function hasEventListener(type:String) : Boolean
        {
            return this.eventDispatcher.hasEventListener(type);
        }

		
        /**
         * 检测在整个事件流是否触发了type事件
         * @param type 事件类型
         * @return 
         */
        public function willTrigger(type:String) : Boolean
        {
            return this.eventDispatcher.willTrigger(type);
        }

		
        /**
         * 单例
         * @return 
         */
        public static function getInstance() : GameDispatcher
        {
            if (instance == null)
            {
                instance = new GameDispatcher;
            }
            return instance;
        }

    }
}
